package com.xgggh.redpack.ui.my.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.xgggh.redpack.R;
import com.xgggh.redpack.base.ui.BaseFragment;
import com.xgggh.redpack.databinding.ContentHomeBinding;
import com.xgggh.redpack.domain.redpack.Pack;
import com.xgggh.redpack.ui.home.viewbinder.HomeViewBinder;
import com.xgggh.redpack.ui.redpack.QrCodeScannerActivity;


public class RedPackDetailsFragment extends BaseFragment {

    ContentHomeBinding mBinding;

    public static RedPackDetailsFragment newInstance() {
        return new RedPackDetailsFragment();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.content_home, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.home, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_camera:
                startActivity(new Intent(getContext(), QrCodeScannerActivity.class));
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //mViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
        // TODO: Use the ViewModel
        adapter.register(Pack.class, new HomeViewBinder());
        adapter.setItems(items);
        mBinding.recyclerView.setAdapter(adapter);
        mBinding.twinklingRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                //getRedPack(firstPage());
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                //getRedPack(nextPage());
            }
        });
        //getRedPack(currentPage());
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //
    }
}
   /* private void getRedPack(final int page){
        getRedPack(page,size,sort);
    }

    private  void getRedPack(final int page,int size,String sort){
        new MockRedPackService(getBaseActivity().getMockDelegate(RedPackService.class))
                //getService(RedPackService.class)
                .getSearchList(page,size,sort)
                .subscribe(new Disposa`bleSubscriber<PackListRsp>() {
                    @Override
                    public void onNext(PackListRsp rsp) {
                        List<Pack> redPacks= rsp.getData();
                        if (page==firstPage()){
                            if (redPacks==null || redPacks.size()==0){
                                // 没有数据，显示空界面，点击跳转发红包界面
                                return;
                            }
                            items.clear();
                            items.addAll(redPacks);
                            adapter.notifyDataSetChanged();
                        }else if (page==nextPage()){
                            if (redPacks==null || redPacks.size()==0){
                                Toast.makeText(getBaseActivity(), "没有更多数据了", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            items.addAll(redPacks);
                            adapter.notifyDataSetChanged();
                        }

                        if (redPacks!=null && redPacks.size()!=0){
                            setCurrentPage(page);
                        }

                    }

                    @Override
                    public void onError(Throwable t) {
                        t.printStackTrace();
                    }

                    @Override
                    public void onComplete() {
                        if (page==firstPage()){
                            mBinding.twinklingRefreshLayout.finishRefreshing();
                        }else{
                            mBinding.twinklingRefreshLayout.finishLoadmore();
                        }
                    }
                });
    }
}
*/